package com.jmb.apiplatform.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.ApiCaseSuiteCaseRefMapper;
import com.jmb.apiplatform.mapper.ApiCaseSuiteMapper;
import com.jmb.apiplatform.pojo.*;
import com.jmb.apiplatform.service.ApiCaseSuiteCaseRefService;
import com.jmb.apiplatform.service.ApiCaseSuiteProcessorService;
import com.jmb.apiplatform.service.ApiCaseSuiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Service
public class ApiCaseSuiteServiceImpl implements ApiCaseSuiteService {
    @Autowired
    private ApiCaseSuiteMapper apiCaseSuiteMapper;
    @Autowired
    private ApiCaseSuiteProcessorService processorService;
    @Autowired
    private ApiCaseSuiteCaseRefService caseRefService;
    @Autowired
    private ApiCaseSuiteCaseRefMapper caseSuiteCaseRefMapper;

    @Override
    public ApiCaseSuite saveApiCaseSuite(ApiCaseSuite caseSuite) throws ValidException {
        if (caseSuite==null){
            throw new ValidException("用例集不能为空");
        }
        apiCaseSuiteMapper.insertApiCaseSuite(caseSuite);
        return caseSuite;
    }

    @Override
    public void saveApiCaseSuiteAndProcessor(ApiCaseSuiteInfoTO apiCaseSuiteInfoTO) throws ValidException {
        // 先插入主表，新增用例
        ApiCaseSuite apiCaseSuite = this.saveApiCaseSuite(apiCaseSuiteInfoTO);

        // 插入附表
        List<ApiCaseSuiteProcessor> suiteProcessors = apiCaseSuiteInfoTO.getSuiteProcessors();
        Date date = new Date();
        if (suiteProcessors!=null && !suiteProcessors.isEmpty()){
            for (ApiCaseSuiteProcessor suiteProcessor : suiteProcessors) {
                suiteProcessor.setCreatedTime(date);
                suiteProcessor.setUpdateTime(date);
                suiteProcessor.setSuiteId(apiCaseSuite.getSuiteId());
                // 缺少插入前置用例
                processorService.saveProcessor(suiteProcessor);
            }
        }

    }

    @Override
    public void editApiCaseSuite(ApiCaseSuiteInfoTO suiteInfoTO) throws ValidException {
        if (suiteInfoTO==null){
            throw new ValidException("用例集不能为空");
        }
        // 修改主表
        apiCaseSuiteMapper.updateApiCaseSuite(suiteInfoTO);
        // 由于前端不方便传主键ID，删除所有的后置处理器后重新新增
        Integer suiteId = suiteInfoTO.getSuiteId();
        processorService.removeApiCaseSuiteProcessorBySuiteId(suiteId);
        Date date = new Date();
        // 获取前置用例信息
        List<ApiCaseSuiteProcessor> suiteProcessors = suiteInfoTO.getSuiteProcessors();
        if (suiteProcessors!=null && !suiteProcessors.isEmpty()){
            for (ApiCaseSuiteProcessor suiteProcessor : suiteProcessors) {
                suiteProcessor.setCreatedTime(date);
                suiteProcessor.setUpdateTime(date);
                suiteProcessor.setSuiteId(suiteId);
                processorService.saveProcessor(suiteProcessor);

            }
        }


    }
    // 删除测试套件
    @Override
    public void removeApiCaseSuiteById(Integer suiteId) throws ValidException {
        // 存在测试用例的不准删除
        ApiCaseSuiteCaseRefTO apiCaseSuiteCaseRefTO = new ApiCaseSuiteCaseRefTO();
        apiCaseSuiteCaseRefTO.setSuiteId(suiteId);
        if (!caseSuiteCaseRefMapper.selectSuiteCaseList(apiCaseSuiteCaseRefTO).isEmpty()){
            log.warn("测试套件下已经存在了测试用例，suiteId={}",suiteId);
            throw new ValidException("测试套件下已经存在了测试用例，suiteId="+suiteId);
        }

        // todo 存在定时任务的也不准删除
        // 删除主表
        apiCaseSuiteMapper.deleteApiCaseSuiteById(suiteId);
        // 删除处理器附表
        processorService.removeApiCaseSuiteProcessorBySuiteId(suiteId);

    }

    /**
     * 获取测试套件详情
     * @param suiteId 测试套件编号
     * @return 测试套件详情
     */
    @Override
    public ApiCaseSuiteVO findApiCaseSuiteById(Integer suiteId) {
        return apiCaseSuiteMapper.selectApiCaseSuiteById(suiteId);
    }

    /**
     * 获取测试套件详情，包含处理器
     * @param suiteId 测试套件编号
     * @return 测试套件详情
     */
    @Override
    public ApiCaseSuiteInfoVO findApiCaseSuiteInfoById(Integer suiteId) {
        return apiCaseSuiteMapper.selectApiCaseSuiteInfoById(String.valueOf(suiteId));
    }

    /**
     * 返回测试套件列表
     * @param apiCaseSuiteTO 用例集查询条件
     * @return 分页后的测试套件列表
     */
    @Override
    public PageResult<ApiCaseSuiteVO> findApiCaseSuiteList(ApiCaseSuiteTO apiCaseSuiteTO) {
        PageHelper.startPage(apiCaseSuiteTO.getPageNum(),apiCaseSuiteTO.getPageSize());
        List<ApiCaseSuiteVO> pageList = apiCaseSuiteMapper.selectApiCaseSuiteList(apiCaseSuiteTO);
        Page<ApiCaseSuiteVO> p = (Page<ApiCaseSuiteVO>)pageList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 获取测试套件列表，不分页
     * @param apiCaseSuiteTO 用例集查询条件
     * @return 不分页的列表，供其他地方调取使用
     */
    @Override
    public List<ApiCaseSuiteVO> findApiCaseSuiteAll(ApiCaseSuiteTO apiCaseSuiteTO) {
        List<ApiCaseSuiteVO> caseSuiteList = apiCaseSuiteMapper.selectApiCaseSuiteList(apiCaseSuiteTO);
        return caseSuiteList;
    }

    /**
     * 复制测试套件
     * @param suiteId 被复制的测试套件编号
     * @param creator 创建人
     * @return 复制的测试用例个数
     */
    @Override
    public HashMap<String, Integer> copyApiCaseSuiteById(Integer suiteId, String creator) throws ValidException {
        Integer cpSuiteCaseCount = 0;
        // 1. 获取复制测试套件信息
        ApiCaseSuiteVO cpSuite = this.findApiCaseSuiteById(suiteId);
        // 2. 获取被测试套件所有的测试用例
        List<ApiCaseSuiteCaseRef> cpSuiteCaseList = caseSuiteCaseRefMapper.selectSuiteAllCase(suiteId);
        // 3. 新增测试套件，获取新增的测试套件编号
        ApiCaseSuite apiCaseSuite = new ApiCaseSuite();
        apiCaseSuite.setSuiteName(cpSuite.getSuiteName());
        apiCaseSuite.setDesc(cpSuite.getDesc());
        Date date = new Date();
        apiCaseSuite.setCreatedTime(date);
        apiCaseSuite.setUpdateTime(date);
        apiCaseSuite.setCreator(creator);
        apiCaseSuite.setExecuteType(cpSuite.getExecuteType());
        apiCaseSuite.setRunDev(cpSuite.getRunDev());
        ApiCaseSuite cpsaveApiCaseSuite = this.saveApiCaseSuite(apiCaseSuite);
        Integer cpSuiteId = cpsaveApiCaseSuite.getSuiteId();
        // 4.往新增测试套件测试用例
        List<ApiCaseSuiteCaseRef> toInsertSuiteCase = new LinkedList<>();
        for (ApiCaseSuiteCaseRef suiteCaseRef : cpSuiteCaseList) {
            suiteCaseRef.setSuiteId(cpSuiteId);
            toInsertSuiteCase.add(suiteCaseRef);
            cpSuiteCaseCount ++;
        }
        if (!toInsertSuiteCase.isEmpty()){
            caseSuiteCaseRefMapper.insertSuiteRef(toInsertSuiteCase);
        }
        HashMap<String, Integer> map = new HashMap<>();
        map.put("cpSuiteCaseCount",cpSuiteCaseCount);

        return map;
    }

}
